#include <cstdio>
#include <unistd.h>
#include <string>
#include <fcntl.h>
#include <cerrno>

void setNoblock(int fd)
{
    int fl = fcntl(fd,F_GETFL);
    if(fl < 0)
    {
        perror("获取文件描述符失败\n");
        return;
    }
    fcntl(fd,F_SETFL,fl | O_NONBLOCK);
}
int main()
{
    std::string echo = "please Echo: ";
    setNoblock(0);
    while(1)
    {
        char buf[1024];
        write(0,echo.c_str(),echo.size());
        int n = read(0,buf,sizeof(buf));
        if(n > 0)
        {
            buf[n] = 0;
            printf("Echo: %s",buf);
        }
        else if(n == 0)
        {
            printf("读到文件结尾，结束\n");
            return 0;
        }
        else
        {
            if(errno == EAGAIN | errno == EINTR)
            {
                printf("文件未准备就绪\n");
                sleep(1);
                continue;
            }
            else
            {
                printf("文件读取异常\n");
                return 0;
            }
            //////do other jobs
        }
    }
    return 0;
}